In this tutorial, we will learn to create a "Hello User" application. User can enter a text into the textbox, then click the button. The application will get the value from the textbox and display the hello message on the screen. The application will also print out the current time and date. ObjectiveThe Object Oriented programming is the most current way of programming. The concept of OO is now applied to not only programming, but also database system, interactive interface, application structure, and various areas. This tutorial will help students to understand how the OO programming works, how convenience the OO programming is. Students will easily start to get into the concept of OO programming and use it into the future programming. Software Requirementü Java JDK, JRE 1.7 or newer ü Android Studio 2.2.3 TutorialFile ----> New Project Give a name for this project “Hello_World”. Make sure the minimum SDK is "API 9: Android 2.3 (Gingerbread). Click 'Next'. 'Next'.
Input the Activity name ”Hello_worldActivity” and the Layout Name “main” In the end, click the “Finish” button
Copy and paste the following code in 'Hello_WorldActivity.java', 'main.xml', 'AndroidManifest.xml' and 'dimen.xml'. Hello_WorldActivity.javapackage android.hello_world;
import android.app.Activity; import android.os.Bundle; import java.text.DateFormat; import java.util.Date; import android.view.View; import android.widget.EditText; import android.widget.TextView;
public class Hello_WorldActivity extends Activity { EditText text1; TextView view1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//this name should //follow your xml file's name in layout document. text1=(EditText)findViewById(R.id.editText1); view1=(TextView)findViewById(R.id.textView1); } public void onclick(View view) { String currentDateTimeString = DateFormat.getDateInstance().format(new Date()); if(text1.getText().toString()==""){ view1.setText("Hello! Default user! \r\n Now is "+currentDateTimeString); } else { view1.setText("Hello! " + text1.getText().toString() + "\r\n Now is " + currentDateTimeString); } } }
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.hello_world" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@mipmap/ic_launcher" android:label="@string/app_name" > <activity android:name=".Hello_WorldActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Hello_WorldActivity" >
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="30dp" android:layout_marginTop="158dp" />
<EditText android:id="@+id/editText1" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" />
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="18dp" android:layout_marginTop="68dp" android:onClick="onclick" android:text="Greeting" />
</RelativeLayout> dimen<?xml version="1.0" encoding="utf-8"?> <resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources> - Save and run the project on the AVD:
- Enter your name into the textbox, click the “Submit” button, the result will be like this:
|
0 Comments